package com.spzx.user.receiver;

import com.rabbitmq.client.Channel;
import com.spzx.common.rabbit.constant.MqConst;
import com.spzx.user.service.IUserInfoService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.io.IOException;

/**
 * @Author ChenXing
 * @Description
 * @Create 2024/8/18 11:07
 */

@Slf4j
@Component
public class UserReceiver {

    @Autowired
    private IUserInfoService userInfoService;

    @SneakyThrows
    @RabbitListener(queues = MqConst.EXCHANGE_TEST)
    public void sendCloseOrderMessage(String orderId, Message message, Channel channel) throws IOException {
        Assert.hasText(orderId, "订单异常");
        try {
            userInfoService.sendCloseOrderMessage(Long.parseLong(orderId));
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
        }
    }


}
